package com.lib.controller.booksearch;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lib.model.dto.BookDto;
import com.lib.model.dto.BookReviewDto;
import com.lib.model.dto.BookSearchDto;
import com.lib.paging.ResultLists;



@Controller
public class NewBookSearch {
	
	@Autowired
	private com.lib.model.service.INewBookSearchService newBookSearchService;
	@Autowired
	private com.lib.model.service.IBookService bookService;
	@Autowired
	private com.lib.model.service.IBookReviewService bookReviewService;

	
	@RequestMapping("/period.do")
	public ModelAndView period(HttpServletRequest request , HttpServletResponse response){
		
		ModelAndView mav = new ModelAndView();
		ResultLists<BookSearchDto> booklist = null;
			
		String sDate =request.getParameter("sDate");
		String eDate = request.getParameter("eDate");
		String sstartPage= request.getParameter("startPage");
		String sstartBlock= request.getParameter("startBlock");
		String searchType = request.getParameter("searchType");
		String searchValue= request.getParameter("searchValue");
		if(searchType != null &&searchType.indexOf('-')>1){
			sDate=searchType;
			eDate=searchValue;
			searchType = null;
			searchValue = null;
		}
		
		
		int startPage = 1;
		int startBlock = 1;
		if (sstartPage != null) {
			startPage = Integer.parseInt(sstartPage);
		}
		if (sstartBlock != null) {
			startBlock = Integer.parseInt(sstartBlock);
		}
		
		if(searchType== null && sDate == null && eDate == null){
			booklist = newBookSearchService.getAllNewBookList(startPage, startBlock, searchType, searchValue);
			mav.addObject("booklist", booklist);
		}
		
		if(searchType!=null){
			booklist = newBookSearchService.getAllNewBookList(startPage, startBlock, searchType, searchValue);
			mav.addObject("booklist", booklist);
			mav.addObject("values",searchType);
		}
		
		if(sDate != null && eDate != null){
			 booklist = newBookSearchService.getAllNewBookList1(startPage, startBlock, sDate, eDate);
			mav.addObject("booklist", booklist);
			System.out.println(booklist.toString());
		}

		mav.setViewName("newsearch");
		
		return mav;
	}
	
	@RequestMapping("/newBookSearch.do")
	public ModelAndView newBookSearch() {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/period.do");
		return mav;
	}
	
	@RequestMapping("/newbookdetail.do")
	public ModelAndView bookdetail(HttpServletRequest request 
													, HttpServletResponse response
													){
		ModelAndView mav = new ModelAndView();


		String book_num = request.getParameter("booknum");
		String sstartPage= request.getParameter("startPage");
		String sstartBlock= request.getParameter("startBlock");
		String searchValue = request.getParameter("searchValue");
		
		if(searchValue != null){
			book_num = searchValue;
		}
			
		int startPage = 1;
		int startBlock = 1;
		if (sstartPage != null) {
			startPage = Integer.parseInt(sstartPage);
		}
		if (sstartBlock != null) {
			startBlock = Integer.parseInt(sstartBlock);
		}
		
		BookDto dto = bookService.getBookDetail(book_num);
		ResultLists<BookReviewDto> reviewList =bookReviewService.getAllReview(startPage,startBlock,book_num);
		
		mav.addObject("bookReview",reviewList);
		mav.addObject("bookDetail",dto);
		mav.setViewName("newbookdetail");
		return mav;
	}
	@RequestMapping("/newaddReview.do")
	public ModelAndView addReview(@RequestParam("book_num")String book_num
													,@RequestParam("id")String id
													,@RequestParam("star")String star
													,@RequestParam("content")String content
													){
		
	
		BookReviewDto rDto = new BookReviewDto(book_num,id,content,Integer.parseInt(star));
		
		bookReviewService.addBookReview(rDto);
	
		ModelAndView mav = new ModelAndView();
		
		mav.setViewName("redirect:/newbookdetail.do?booknum="+book_num);
		
		return mav;
	}
	@RequestMapping("/newdelReview.do")
	public ModelAndView delReview(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mav = new ModelAndView();
		
		String book_num = request.getParameter("book_num");
		String id = request.getParameter("id");
		
		BookReviewDto dto = new BookReviewDto();
		dto.setBook_num(book_num);
		dto.setId(id);
		
		bookReviewService.delReview(dto);
		
		mav.setViewName("redirect:/newbookdetail.do?booknum="+book_num);
		return mav;
	}
}
